package com.xuzimian.globaldemo.aws.cloud.web.controller;

import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.model.Credentials;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.xuzimian.globaldemo.aws.cloud.s3.common.AWSTokenServiceHelper.getCredentials;
import static com.xuzimian.globaldemo.aws.cloud.s3.common.AWSTokenServiceHelper.getStsService;

@RestController
@RequestMapping("/sts")
public class AWSSecurityTokenController {

    @GetMapping("/getcredentials")
    public ResponseEntity<Credentials> getStsCredentials() {
        AWSSecurityTokenService sts_client = getStsService("https://sts.us-east-2.amazonaws.com/",
                "us-east-2");

        Credentials session_creds = getCredentials(sts_client);

        return new ResponseEntity<Credentials>(session_creds, HttpStatus.OK);
    }
}
